#include "game.h"

game_t game;

void init_game(void)
{
	com_memset(&game, 0, sizeof(game));

	player_t * p = &game.player[0];
	game.player_count = 1;

	strcpy(p->name, "ball");
	p->model = rd_load_model("man");
	p->speed = 200;
	p->state = 0;
	p->w = 40;
	p->h = 40;

	Timer_start(&game.tick);
}

void release_game(void)
{
	;
}

//16方向贴图
static void calc_16direction(player_t * p)
{
	if (p->vel_x == 0) {
		if (p->vel_y != 0) {
			p->direction = (p->vel_y > 0) ? 8 : 0;
		}
		//如果没有速度，则方向不变
		return ;
	} else {
		float ratio = p->vel_y / p->vel_x;
		if (p->vel_x > 0) {
			if (ratio < -5.027)
				p->direction = 0;
			else if (ratio < -1.496)
				p->direction = 1;
			else if (ratio < -0.668)
				p->direction = 2;
			else if (ratio < -0.199)
				p->direction = 3;
			else if (ratio < 0.199)
				p->direction = 4;
			else if (ratio < 0.668)
				p->direction = 5;
			else if (ratio < 1.496)
				p->direction = 6;
			else if (ratio < 5.027)
				p->direction = 7;
			else
				p->direction = 8;
		} else {
			if (ratio < -5.027)
				p->direction = 8;
			else if (ratio < -1.496)
				p->direction = 9;
			else if (ratio < -0.668)
				p->direction = 10;
			else if (ratio < -0.199)
				p->direction = 11;
			else if (ratio < 0.199)
				p->direction = 12;
			else if (ratio < 0.668)
				p->direction = 13;
			else if (ratio < 1.496)
				p->direction = 14;
			else if (ratio < 5.027)
				p->direction = 15;
			else
				p->direction = 0;
		}
	}
}

//8方向贴图
static void calc_8direction(player_t * p)
{
	if (p->vel_x == 0) {
		if (p->vel_y != 0) {
			p->direction = (p->vel_y > 0) ? 6 : 2;
		}
		//如果没有速度，则方向不变
		return ;
	} else {
		float ratio = p->vel_y / p->vel_x;
		if (p->vel_x > 0) {
			if (ratio < -2.414)
				p->direction = 2;
			else if (ratio < -0.414)
				p->direction = 3;
			else if (ratio < 0.414)
				p->direction = 4;
			else if (ratio < 2.414)
				p->direction = 5;
			else
				p->direction = 6;
		} else {
			if (ratio < -2.414)
				p->direction = 6;
			else if (ratio < -0.414)
				p->direction = 7;
			else if (ratio < 0.414)
				p->direction = 0;
			else if (ratio < 2.414)
				p->direction = 1;
			else
				p->direction = 2;
		}
	}
}

static void player_walk(player_t * p)
{
	if (p->path > p->model->step[0]) {
		//换下一帧
		p->frame = (p->frame + 1) % p->model->frames[0];
		p->path = 0;
	}
	if (p->vel_x == 0 && p->vel_y == 0) {
		p->frame = 0;
	}
}

static void update_player(player_t * p, int interval)
{
	//计算分速度
	float len_x = p->dist_x - p->x - 0.5*p->w;
	float len_y = p->dist_y - p->y - 0.5*p->h;
	if (fabs(len_x) > 3 || fabs(len_y) > 3) {
		float ratio = p->speed / sqrt(len_x*len_x+len_y*len_y);
		p->vel_x = ratio * len_x;
		p->vel_y = ratio * len_y;
	} else {
		p->vel_x = 0;
		p->vel_y = 0;
	}

	//更新位置
	int step_x, step_y;

	step_x = p->vel_x * (interval / 1000.f);
	p->x += step_x;
	step_y = p->vel_y * (interval / 1000.f);
	p->y += step_y;

	//消除玩家与地图的碰撞
	g_decollide_player_map(p, step_x, step_y);

	//距上一步已移动路程
	p->path += sqrt(step_x*step_x+step_y*step_y);

	//切换帧
	switch (p->state) {
		case 0:
			player_walk(p);
			break;
		case 1:
			break;
		case 2:
			break;
		default:
			break;
	}

	calc_16direction(p);
}

void g_update(void)
{
	static int last_tick;
	int interval = Timer_now(&game.tick) - last_tick;

	update_player(&game.player[0], interval);

	last_tick = Timer_now(&game.tick);
}

void g_pause(void)
{
	Timer_pause(&game.tick);
}


